package com.dingding.service.recruit.core.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.dingding.common.core.beans.RecruitUser;
import com.dingding.common.core.util.paravalidate.ValiResult;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.facde.recruit.dto.DriverRecruitCityDTO;
import com.dingding.facde.recruit.dto.ReqGetDriverRecruitInfoDTO;
import com.dingding.facde.recruit.dto.ReqSignUpDTO;
import com.dingding.facde.recruit.dto.ReqValidateCode;
import com.dingding.facde.recruit.dto.ResGetDriverRecruitInfoDTO;
import com.dingding.facde.recruit.dto.ResValidateCode;
import com.dingding.service.recruit.core.dao.IDriverRecruitDao;
import com.dingding.service.recruit.core.service.IDriverRecruitService;
import com.dingding.service.share.dao.IActivityShareDao;

/**
 * 
 * @ClassName: DriverRecruitServiceImpl 
 * @Description: 司机招募业务逻辑接口实现类 
 * @author YHQ
 * @date 2016年11月11日 下午3:16:16
 * @since V2.5.6
 * 
 */
@Component("driverRecruitService")
public class DriverRecruitServiceImpl implements IDriverRecruitService{

	@Resource(name = "driverRecruitDao")
	private IDriverRecruitDao driverRecruitDao;
	
	@Resource(name = "activityShareDao")
	private IActivityShareDao activityShareDao;
	
	/**
	 * 
	 * @Title: getDriverRecruitCity 
	 * @Description: 获取司机招募城市
	 * @author YHQ 
	 * @since V2.5.6
	 * @return List<DriverRecruitCityDTO>    返回类型
	 * 
	 */
	@Override
	public List<DriverRecruitCityDTO> getDriverRecruitCity() {
		
		return driverRecruitDao.getDriverRecruitCity();
	}

	/**
	 * 
	 * @Title: getDriverRecruitInfo 
	 * @Description: 获取司机招募报名信息
	 * @param recruitInfo 客户端请求Bean
	 * @author YHQ 
	 * @since V2.5.6
	 * @return ResGetDriverRecruitInfo    返回类型
	 * 
	 */
	@Override
	public ResGetDriverRecruitInfoDTO getDriverRecruitInfo(ReqGetDriverRecruitInfoDTO recruitInfo) {
		
		// 判断客户端请求参数是否为空
		if(recruitInfo == null || !validateParam(recruitInfo)){
			return null;
		}
		
		return driverRecruitDao.getDriverRecruitInfo(recruitInfo.getTelPhone());
	}

	/**
	 * 
	 * 验证客户端请求参数是否合法
	 * @Title: validateParam 
	 * @author YHQ
	 * @since V2.5.6
	 * 
	 */
	private boolean validateParam(Object object) {

		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(object, object.getClass());

		if (valiResult != null && !valiResult.isAvailable()) {// 参数为空
			return false;
		}

		return true;
	}
	
	/**
	 * 
	 * @Title: driverSignUp 
	 * @Description: 招募司机报名
	 * @param @param reqSignUpDTO 客户端请求Bean
	 * @author YHQ 
	 * @since V2.5.6
	 * @return int    返回类型
	 * 
	 */
	@Override
	public boolean driverSignUp(ReqSignUpDTO reqSignUpDTO) {
		
		// 判断客户端请求参数是否为空
		if(reqSignUpDTO == null || !validateParam(reqSignUpDTO)){
			return false;
		}
		
		// 获取招募司机报名信息
		RecruitUser recruitUser = getRecruitUser(reqSignUpDTO);
		
		if(recruitUser == null){
			return false;
		}
		
		return driverRecruitDao.driverSignUp(recruitUser);
	}
	
	/**
	 * 
	 * @Title: getRecruitUser 
	 * @Description: 将客户端请求Bean转为司机招募Bean
	 * @param reqSignUpDTO 客户端请求Bean
	 * @author YHQ 
	 * @since V2.5.6
	 * @return RecruitUser    返回类型
	 * 
	 */
	public RecruitUser getRecruitUser(ReqSignUpDTO reqSignUpDTO){
		
		RecruitUser recruitUser = new RecruitUser();
		
		// 将客户端请求Bean赋予recruitUser
		BeanUtils.copyProperties(reqSignUpDTO, recruitUser);
		
		if(reqSignUpDTO.getRecId() == null){
			recruitUser.setRecId(null);
		}
		
		// 报名日期
		recruitUser.setCreateDate(new Date());
		// 审核状态
		recruitUser.setExamStatus(1);
		
		return recruitUser;
	}

	/**
	 * 
	 * @Title: validateCode 
	 * @Description: 验证手机验证码
	 * @param reqValidateCode 客户端请求Bean
	 * @author YHQ 
	 * @since V2.5.6
	 * @return ResValidateCode    返回类型
	 * 
	 */
	@Override
	public ResValidateCode validateCode(ReqValidateCode reqValidateCode) {
		
		// 判断客户端请求参数是否为空
		if(reqValidateCode == null || !validateParam(reqValidateCode)){
			return null;
		}
		
		// 验证手机号是否为空
		boolean flag = activityShareDao.isMsgCodeTrue(reqValidateCode.getTelPhone(), reqValidateCode.getCode());
		
		if(flag){
			
			// 获取审核状态
			ResValidateCode resValidateCode = driverRecruitDao.getRecruitUser(reqValidateCode.getTelPhone());
			
			if(resValidateCode == null){
				
				resValidateCode = new ResValidateCode();
				resValidateCode.setExamStatus(0);
			}else if(resValidateCode.getExamStatus() == null){
				resValidateCode.setExamStatus(0);
			}
			
			return resValidateCode;
		}
		
		return null;
	}
	
}
